<template>
    <div>
        <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row>
        <el-col>
          <el-button type="primary" @click="showAddCateDialog">添加分类</el-button>
        </el-col>
      </el-row>

      <!-- 表格 -->
      <tree-table class="treeTable" :data="catelist" :columns="columns" :selection-type="false" :expand-type="false" show-index index-text="#" border :show-row-hover="false">
        <!-- 是否有效 -->
        <template slot="isok" slot-scope="scope">
          <i class="el-icon-success" v-if="scope.row.cat_deleted === false" style="color: lightgreen;"></i>
          <i class="el-icon-error" v-else style="color: red;"></i>
        </template>
        <!-- 排序 -->
        <template slot="order" slot-scope="scope">
          <el-tag size="mini" v-if="scope.row.cat_level===0">一级</el-tag>
          <el-tag type="success" size="mini" v-else-if="scope.row.cat_level===1">二级</el-tag>
          <el-tag type="warning" size="mini" v-else>三级</el-tag>
        </template>
        <!-- 操作 -->
        <template slot="opt" slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit" size="mini">编辑</el-button>
          <el-button type="danger" icon="el-icon-delete" size="mini">删除</el-button>
        </template>
      </tree-table>

      <!-- 分页区域 -->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="querInfo.pagenum" :page-sizes="[3, 5, 10, 15]" :page-size="querInfo.pagesize" layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </el-card>

    <!-- 添加分类的对话框 -->
    <el-dialog title="添加分类" :visible.sync="addCateDialogVisible" width="50%" @close="addCateDialogClosed">
      <!-- 添加分类的表单 -->
      <el-form :model="addCateForm" :rules="addCateFormRules" ref="addCateFormRef" label-width="100px">
        <el-form-item label="分类名称：" prop="cat_name">
          <el-input v-model="addCateForm.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="父级分类：">
          <!-- options 用来指定数据源 -->
          <!-- props 用来指定配置对象 -->
          <el-cascader 
          expandTrigger="hover"
          :options="parentCategoryList" 
          :props="cascaderProps" 
          v-model="selectedKeys" 
          @change="parentCateChanged" 
          clearable>
          </el-cascader>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addCateDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addCate">确 定</el-button>
      </span>
    </el-dialog>
    </div>
</template>
<script>
export default {
    data(){
        return{
           catelist:[],
            querInfo:{
                type:3,
                pagenum:1,
                pagesize:5,               
            },
            total:0,
             // 为table指定列的定义
            columns: [
                {
                label: '分类名称',
                prop: 'cat_name'
                },
                {
                label: '是否有效',
                // 表示，将当前列定义为模板列
                type: 'template',
                // 表示当前这一列使用模板名称
                template: 'isok'
                },
                {
                label: '排序',
                // 表示，将当前列定义为模板列
                type: 'template',
                // 表示当前这一列使用模板名称
                template: 'order'
                },
                {
                label: '操作',
                // 表示，将当前列定义为模板列
                type: 'template',
                // 表示当前这一列使用模板名称
                template: 'opt'
                }
            ],
            addCateDialogVisible:false,
            //表单的数据项
            addCateForm:{
                cat_name:'',
                // 父级分类的Id
                cat_pid: 0,
                // 分类的等级，默认要添加的是1级分类
                cat_level: 0
            },
            //表单的校验规则
            addCateFormRules:{
                cat_name:[
                    {required: true, message: '请输入分类名称', trigger: 'blur'}
                ]
            },
            //父分类的列表
            parentCategoryList:[],
            cascaderProps:{
                value: 'cat_id',
                label: 'cat_name',
                children: 'children'
            },
            //绑定的父节点的值
            selectedKeys: []

        }
    },
    methods:{
        //获取分类信息
        async getCategoryList(){
          const {data :res} =  await this.$http.get('categories',{params: this.querInfo})
          if(res.meta.status != 200) return this.$message.error(res.meta.msg)
            this.catelist = res.data.result;
            this.total = res.data.total;
            this.$message.success(res.meta.msg)
      },
      //监听页面大小发生变化
        handleSizeChange(newSize){
            this.querInfo.pagesize=newSize
            this.getCategoryList()
        },
        //监听页码发生变化
        handleCurrentChange(newPage){
            this.querInfo.pagenum = newPage;
            this.getCategoryList();
        },
        //添加分类对话框打开
        showAddCateDialog(){
            this.addCateDialogVisible = true;
            this.getParentsCategory();
        },
        async getParentsCategory(){
            const {data :res} =  await this.$http.get('categories',{params: {type:2}})
            if(res.meta.status != 200) return this.$message.error(res.meta.msg)
            this.parentCategoryList = res.data;
            console.log(this.parentCategoryList);
            this.$message.success(res.meta.msg)
        },
        //父节点发生变化时子节点的值要变化
        parentCateChanged(){
            console.log('keys');
            console.log(this.selectedKeys);
            //当selectedKey的长度为0时，没有父级分类
            if(this.selectedKeys.length>0){
                this.addCateForm.cat_pid = this.selectedKeys[this.selectedKeys.length-1]
                this.addCateForm.cat_level = this.selectedKeys.length;
            }else{
                this.addCateForm.cat_pid = 0;
                this.addCateForm.cat_level=0;
            }
        },
        //关闭对话框
        addCateDialogClosed(){
            this.addCateDialogVisible = false
        },
        //分类选择父级更改
        parentCateChanged(){
            
        },
        //添加分类
        addCate(){
            console.log(this.addCateForm);
        }
    },
    created(){
      this.getCategoryList();
    }
}
</script>
<style lang="less" scoped>
.treeTable {
  margin-top: 15px;
}

.el-cascader {
  width: 100%;
}
</style>